<?php
namespace app\index\controller;
use think\Controller;
use think\Request;

class Base extends Controller {
	var $uinfo;
	var $postData;
	var $token;
	var $timestamp;
	public function _initialize() {
		$noLoginAction = ['index/yhq_detail'];
		$statics = APP_URL . "/public/static/";
		$this->assign("app_url", APP_URL);
		$this->assign("statics", $statics);
		$this->assign("css_path", $statics . "css/");
		$this->assign("images_path", $statics . "images/");
		$this->assign("js_path", $statics . "js/");
		$this->assign("base_path", APP_URL . "/public/");
		$this->postData = Request::instance()->param();
		$user_agent = Request::instance()->header("user-agent");

		if (isset($this->postData['device']) && $this->postData['device'] == 'wxmp') {
			define('DEVICE', 'wxmp');
		} elseif (strpos($user_agent, "Coco3gAppAndroid") !== false) {
			define("DEVICE", "android");
		} elseif (strpos($user_agent, "Coco3gAppIOS") !== false) {
			define("DEVICE", "ios");
		} else {
			define("DEVICE", "mobile");
		}
		$this->assign("device", DEVICE);
		// $this->assign("layname",$layname);

		/* 读取数据库中的配置 */
		$config = cache('DB_CONFIG_DATA');
		if (!$config) {
			$config = model('Config')->lists();
			cache('DB_CONFIG_DATA', $config);
		}
		config($config); //添加配置

		$this->assign("paramData", $this->postData);
		if (!isset($_SERVER['PATH_INFO'])) {
			header("HTTP/1.1 404 Not Found");
			exit;
		}
		$url = $_SERVER['PATH_INFO'];

		$urlarr = explode('/', trim($url, '/'));
		$nowurl = $urlarr[1] . '/' . $urlarr[2];
		if (!in_array(strtolower($urlarr[1]), ['cron', 'test']) && !in_array($nowurl, $noLoginAction)) {
			$this->check_auth();
		}
		$this->assign("token", $this->token);
		$this->assign("timestamp", $this->timestamp);
	}
	protected function check_auth() {
		$token = Request::instance()->header('token');
		$this->token = isset($token) ? $token: $this->postData['token'];
		$timestamp = Request::instance()->header('timestamp');
		$this->timestamp = $timestamp ?: $this->postData['timestamp'];
		if (!$this->token) {
			$this->token = session("token");
		} else {
			session("token", $this->token);
		}
		if (!$this->timestamp) {
			$this->timestamp = SYS_TIME;
		}
		$uinfo = model("MemberToken")->check_token($this->token, $this->timestamp);
		if ($uinfo !== false) {
			$this->uinfo = $uinfo;
			$this->assign("uinfo", $uinfo);
			$this->assign("_userid", $uinfo['userid']);
		} else {
			model("MemberToken")->delete_token($token);
			return false;
		}
		return true;
	}
	// protected function check_auth(){
	//     $token = Request::instance()->header('token');
	//     $timestamp = Request::instance()->header('timestamp');
	//     $this->assign("token",$token);
	//     $this->assign("timestamp",SYS_TIME);
	//     $uinfo = model("MemberToken")->check_token($token,$timestamp);
	//     if($uinfo !== false){
	//         $this->uinfo = $uinfo;
	//         $this->assign("uinfo",$uinfo);
	//         $this->assign("_userid",$uinfo['id']);
	//     }
	//     else{
	//         model("MemberToken")->delete_token($token);
	//         return false;
	//     }
	//     return true;
	// }
}